<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    >
      <template v-if="meta.indexOf('MemberRechargeOrderListExcel') > -1" #btns>
        <el-button
          :loading="exportLoading"
          type="primary"
          @click="batchexport"
        >{{ $t('message.daochu') }}</el-button>
      </template>
    </QueryForm>
    <div v-if="tjList && tjList.length > 0" class="total_wrap">
      <div class="first item">
        <div class="key">{{ $t('message.shanxuan') }}</div>
        <div class="key">{{ $t('message.heji') }}</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.payMethod }}</div>
          <div class="value">
            <!-- <span>{{ $t('message.rmbfh') }}</span> -->
            <span class="price">{{ item.money }}</span>
          </div>
        </div>
      </div>
      <el-tooltip effect="dark" placement="left">
        <div class="jesm">
          <img
            style="width: 15px; margin-right: 2px"
            src="@/assets/icon/question.png"
            alt=""
          >{{ $t('message.jesm') }}
        </div>
        <div slot="content" v-html="smTip" />
      </el-tooltip>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="$t('message.czddh')"
        prop="orderId"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ddlx')"
        prop=""
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.orderTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.memberInfo')"
        min-width="240"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.nickName +
              "-" +
              scope.row.phone +
              "-" +
              scope.row.memberCard
          }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.czqd')" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.channelTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('message.czje')"
        prop="originalPrice"
        min-width="100"
        align="center"
      />
      <el-table-column
        :label="$t('message.zsje')"
        prop="giveMoney"
        width="100"
        align="center"
      />
      <el-table-column
        align="center"
        :label="$t('message.czfs')"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ payMethod(scope.row.payMethod) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('message.cgmc')"
        prop="venueName"
        width="120"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        :label="$t('message.czsj2')"
        prop="createTime"
        min-width="160"
        align="center"
      />
      <el-table-column
        :label="$t('message.czr')"
        prop="creatorName"
        min-width="100"
        align="center"
        show-overflow-tooltip
      />
    </el-table>
    <Pagination
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { channelConfigPost, configList, rechargeOrderb, MemberRechargeOrderListExcel } from '@/api'
import { parseTime } from '@/utils'
import dayjs from 'dayjs'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      exportLoading: false,
      venueList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      totalCount: 10,
      allPayMethedList: [],
      list: [],
      listLoading: false,
      filters: { time: [] },
      tjList: [],
      meta: [],
      smTip: ''
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.init()
    this.getVenueList()
    this.getChannel()
    // this.getOrderType()
  },
  methods: {
    // getOrderType() {
    //   orderTypeList({ param: { code: 'ORDER_TYPE_MEMBER_CARD' }}).then(res => {
    //     this.queryFormConfig.formItems[7].options = res.recordList || []
    //   })
    // },
    getChannel() {
      channelConfigPost({ param: { code: 'CHANNEL_CARD' }}).then(res => {
        this.queryFormConfig.formItems[5].options = res.recordList || []
      })
    },
    init() {
      let time = []
      const start = new Date()
      const end = new Date()
      start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
      time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
      this.$set(this.filters, 'time', time)
      this.getList(1)
    },
    payStatus(status) {
      switch (status) {
        // case '0':
        //   return this.$t('message.dzf')
        // case '1':
        //   return this.$t('message.cswzf')
        // case '2':
        //   return this.$t('message.yqx')
        case '3':
          return this.$t('message.zfcg')
        default:
          return this.$t('message.yqx')
      }
    },
    payMethod(status) {
      // 01-会员卡,02-现金,03-微信,04-支付宝,05微信扫码支付
      const obj = this.allPayMethedList.find(item => item.value === status)
      if (obj) {
        return obj.name
      } else {
        return '-'
      }
    },
    // 获取场馆列表
    getVenueList() {
      configList({ param: { type: '2', code: '' }}).then(res => {
        if (res.errorCode !== '000000') return
        const arr = res.recordList
        const allPayMethedList = []
        arr.forEach(s => {
          if (s.value !== '01') {
            allPayMethedList.push(s)
          }
        })
        this.allPayMethedList = allPayMethedList
        this.queryFormConfig.formItems[0].options = allPayMethedList
      }).catch(err => {
        console.log(err)
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
        this.pagination.firstQueryTime = parseTime(new Date())
      }
      if (filters.time && filters.time.length > 0) {
        filters.startDate = filters.time[0]
        filters.endDate = filters.time[1]
      } else {
        filters.startDate = ''
        filters.endDate = ''
      }
      filters.venueId = sessionStorage.getItem('venueId')
      rechargeOrderb({
        pagination,
        param: { ...filters }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
            const tjList = res.data
            if (tjList) {
              let smTip = ''
              tjList.forEach(s => {
                smTip = smTip + s.tip + '<br />'
              })
              this.tjList = tjList
              this.smTip = smTip
            }
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    clear() {
      this.filters = {
        time: this.filters.time
      }
      this.getList(1)
    },
    batchexport() {
      const { filters } = this
      if (filters.time && filters.time.length > 0) {
        filters.startDate = filters.time[0]
        filters.endDate = filters.time[1]
      } else {
        filters.startDate = ''
        filters.endDate = ''
      }
      filters.venueId = sessionStorage.getItem('venueId')
      this.exportLoading = true
      MemberRechargeOrderListExcel({ param: { ...filters }, shopId: localStorage.getItem('shopId') }).then(res => {
        this.exportLoading = false
        if (res.errorCode === '000000') {
          window.open(res.record.showUrl, '_blank')
        }
      }).catch(() => {
        this.exportLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.vip-info {
  display: flex;
  justify-content: center;
  .name-phone {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: left;
    margin-left: 10px;
  }
}
</style>
